"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskNewreportCtrl
 * @description
 * # TaskNewreportCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "TaskNewreportCtrl",
    function (DataService, $interval, localStorageService, $routeParams, $filter, openApi, Util, $location) {
      // 设置工作报告自定义字段的formkey
      Util.setformKey("Personnel_WorkReport");
      var stop = -1;
      var vm = this;
      var pkId = $routeParams.pkId;
      this.getMenuData = function () {
        var param = {};
        param.type = "工作报告类型";
        param.cache = true;
        DataService.getData(openApi.getKeyType, param).then(function (data) {
          vm.reportTypelist = data;
        });
      };
      this.choosepeople = function () {
        // 选人时候设置上次加载为false
        vm.param.hasGetFollowed = false;
        var userskey = {
          iconCls: "portrait",
          name: "personName",
          id: "userId"
        };
        localStorageService.set("userskey", userskey);
        localStorageService.set("chooseUsrs", vm.param.followList);
        localStorageService.set("reportDetail", vm.param);
        // getFollows是要访问的接口
        $location.path("chooseuser/index/getFollows");
      };
      this.submit = function () {
        submitReport(1);
      };
      // 保存到草稿
      this.savetodraft = function () {
        submitReport(0);
      };
      // 上次选择的人
      var getlastuserRemote = function () {
        if (!vm.param.hasGetFollowed) {
          return
        }
        var param = {};
        param.reportType = vm.param.reportType;
        param.noloader = true;
        DataService.getData(
          openApi.workReoprtFollow,
          param
        ).then(function (data) {
          if (data) {
            vm.param.followList = data;
          } else {
            Util.Toast("没有找到上次信息");
            vm.param.hasGetFollowed = !vm.param.hasGetFollowed;
          }
        });
      };

      // 上次选择的人
      this.getlastuser = function () {
        vm.param.hasGetFollowed = !vm.param.hasGetFollowed;
        if (vm.param.hasGetFollowed) {
          getlastuserRemote()
        } else {
          vm.param.followList = [];
        }
      };
      var submitReport = function (status) {
        if (!vm.param.reportDate) {
          Util.Toast("请选择报告日期");
          return;
        }
        if (!vm.param.content) {
          Util.Toast("请填写已完成的工作");
          return;
        }
        if (!vm.param.unfinishedWork) {
          Util.Toast("请填写未完成的工作");
          return;
        }
        if (!vm.param.assistedWork) {
          Util.Toast("请填写需要协助的工作");
          return;
        }
        if (!vm.param.difficultWork) {
          Util.Toast("请填写遇到的困难");
          return;
        }
        if (!vm.param.nextWork) {
          Util.Toast("请填写下一步的工作");
          return;
        }
        
        if (!(vm.param.followList && vm.param.followList.length > 0)) {
          Util.Toast("请添加报告关注人");
          return;
        }
        if (!vm.validation.passvalidation) {
          Util.Toast(vm.validation.tip);
          return;
        }
        vm.param.follows = vm.param.followList.map(function (item, index) {
          return item.id || item.userId;
        });
        var param = angular.copy(vm.param);
        delete param.followList;
        delete param.actions;
        param.status = status;
        // 根据pkid来判断是新增还是更新
        var url = openApi.addReport;
        if (pkId) {
          url = openApi.updateReport;
        }
        DataService.getData(url, param).then(
          (function (data) {
            vm.param={};
            localStorageService.remove("reportDetail");
            $interval.cancel(stop)
            Util.Toast(data, { callback: Util.goBack });
          }).bind(this)
        );
      };
      this.delpersons = function (index) {
        this.param.followList.splice(index, 1);
      };
      var getParams = function () {
        vm.param = localStorageService.get("reportDetail");
        if (!vm.param) {
          vm.param = {};
          vm.param.reportType = "日报";
          vm.param.reportDate = $filter("dateFmt")(new Date());
        }
        vm.param.followList = localStorageService.get("chooseUsrs");
        localStorageService.remove("chooseUsrs");
      };
      var getReoprtData = function () {
        if (pkId && !vm.param.haveGetData) {
          var param = {};
          param.pkId = pkId;
          DataService.getData(
            openApi.getworklogDetail,
            param
          ).then(function (data) {
            vm.param = data;
            // 已经获取了接口数据，不需要在获取接口数据了
            vm.param.haveGetData = true;
          });
        }
      };
      var initdata = function () {
        vm.getMenuData();
        getParams();
        getReoprtData();
        getlastuserRemote()
        stop = $interval(function () {
          if (vm.param) {
            localStorageService.set("reportDetail", vm.param);
          }
        }, 100);
      };
      initdata();
    }
  );
